import {
	roleDict
} from '../config'

export const anitShakeExecuteCallback = (callback, globalAntiShakeInterval = 300) => {
	// globalAntiShakeInterval = globalAntiShakeInterval ? globalAntiShakeInterval : 300;
	if (typeof (callback) != 'function') {
		console.log(
			`5-5 common.js anitShakeExecuteCallback::::: callback is not a function...`);
		return;
	}
	// return function() {
	if (getApp().globalData.globalAntiShakeTimer !== null) {
		clearTimeout(getApp().globalData.globalAntiShakeTimer);
	}
	getApp().globalData.globalAntiShakeTimer = setTimeout(callback, globalAntiShakeInterval);
	// }
}

export const valueWatch = function (e, value = undefined) { // 实际使用时需要 用apply或者call 改this

	let name = e.currentTarget.dataset.name; // 实际触发时间的元素 e.currentTarget 上一定要有 data-name 属性, 这用来代表 data 下 点标示法的路径 比如 data-name="a.b" 代表 data.a.b ， 而 data-name="a" 代表 data.a
	value = value ? value : e.detail.value;
	console.log(`18-18 utils/common.js:::::valueWatch:::name:${name} , value:${value} , e:`, e);
	this.setData({
		[name]: value, //把 e.detail.value 的值设定到 data[name] 上， name是变量名而值就是上边 data-name 的值 
	})
}

export const getNowFormatDate = function (date=null, needHMS = true) { //获取当前时间,返回时间格式：2018-09-16 15:43:36
	date = date ? date : new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	function ensureTwoDiget(dtNum){
		dtNum = dtNum.toString();
		if(dtNum.length != 2){
			dtNum = '0' + dtNum;
		}
		return dtNum;
	}
	month = ensureTwoDiget(month)
	strDate = ensureTwoDiget(strDate)
	let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
	console.log(`43-43 utils/getNowFormatDate currentdate: ${currentdate} , needHMS: ${needHMS}`);
	if (needHMS) {
		var hour = ensureTwoDiget(date.getHours());
		var min = ensureTwoDiget(date.getMinutes());
		var sec = ensureTwoDiget(date.getSeconds());
		currentdate += " " + hour + seperator2 + min+
			seperator2 + sec;
	}
	return currentdate;
}

export const getIdShowTextFromUserInfo = function (userInfo) {
	const userid = userInfo['userid'];
	const preid = roleDict[userInfo.role].preid;
	const idShowText = `${preid}${userid}`;
	return idShowText;
}

export const checkUserInfo = function (userInfo) {
	console.log(`58-58 common.js method:::::checkUserInfo:::userInfo, `, userInfo);
	if (Object.prototype.toString.call(userInfo) != '[object Object]') {
		console.log(`56-56 util/common.js userInfo is not obj... return false...`);
		return false;
	} else if (!('wxopenid' in userInfo)) {
		console.log(`56-56 util/common.js userInfo has not key wxopenid... return false...`);
		return false;
	} else if (!('userid' in userInfo)) {
		console.log(`56-56 util/common.js userInfo has not key userid... return false...`);
		return false;
	} else if (!('role' in userInfo)) {
		console.log(`56-56 util/common.js userInfo has not key role... return false...`);
		return false;
	} else if (!('school' in userInfo)) {
		console.log(`56-56 util/common.js userInfo has not key school... return false...`);
		return false;
	} else {
		console.log(`56-56 util/common.js userInfo passed obj type check and ['wxopenid','userid','role','school'] key check... return false...`);
		return true
	}
}

export const setUserInfoIntoStorage = function (userInfo) {
	let userInfoDict = wx.getStorageSync('userInfoDict');
	userInfoDict = userInfoDict ? userInfoDict : {};
	if (checkUserInfo(userInfo)) {
		userInfoDict[userInfo.wxopenid] = userInfo;
		wx.setStorageSync('wxopenid', userInfo.wxopenid);
		wx.setStorageSync('userInfoDict', userInfoDict);
		return true;
	} else {
		console.log(`60-60 common.js method:::::setUserInfoIntoStorage:::  set failed...  got invalid userInfo:`, userInfo);
		return false;
	}
}

export const getUserInfoFromStorage = function () {
	let wxopenid = wx.getStorageSync('wxopenid');
	const userInfoDict = wx.getStorageSync('userInfoDict');
	const userInfo = userInfoDict[wxopenid];
	if (checkUserInfo(userInfo)) {
		return userInfo;
	} else {
		console.log(`58-58 common.js method:::::getUserInfoFromStorage:::   got invalid userInfo:`, userInfo);
		return false;
	}
}

export const injectUserInfoFromStorageIntoData = function () { // 需要用 call 或者 apply 改this
	const userInfo = getUserInfoFromStorage();
	if (!checkUserInfo(userInfo)) {
		console.log(`106-106 utils/common.js function:::::injectUserInfoFromStorageIntoData::: invalid userInfo got...`, userInfo);
		return false
	} else {
		const roleModuleName = roleDict[userInfo.role].moduleName;
		const diffIdKey = `${roleModuleName}_id`;
		this.setData({
			userInfo,
			[diffIdKey]: userInfo.userid,
		});
		return true
	}
}